﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace VirusCleaner.Tray
{

    public partial class FrmTray : Form
    {

        #region U盘检测
        protected override void WndProc(ref Message m)
        {
            #region 常量函数
            const int WM_DEVICECHANGE = 0x219;
            const int DBT_DEVICEARRIVAL = 0x8000;
            const int DBT_CONFIGCHANGECANCELED = 0x0019;
            const int DBT_CONFIGCHANGED = 0x0018;
            const int DBT_CUSTOMEVENT = 0x8006;
            const int DBT_DEVICEQUERYREMOVE = 0x8001;
            const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
            const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
            const int DBT_DEVICEREMOVEPENDING = 0x8003;
            const int DBT_DEVICETYPESPECIFIC = 0x8005;
            const int DBT_DEVNODES_CHANGED = 0x0007;
            const int DBT_QUERYCHANGECONFIG = 0x0017;
            const int DBT_USERDEFINED = 0xFFFF;

            #endregion;
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case WM_DEVICECHANGE:
                            break;
                        case DBT_DEVICEARRIVAL://U盘插入
                            break;
                        case DBT_CONFIGCHANGECANCELED:
                            break;
                        case DBT_CONFIGCHANGED:
                            break;
                        case DBT_CUSTOMEVENT:
                            break;
                        case DBT_DEVICEQUERYREMOVE:
                            break;
                        case DBT_DEVICEQUERYREMOVEFAILED:
                            break;
                        case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                            break;
                        case DBT_DEVICEREMOVEPENDING:
                            break;
                        case DBT_DEVICETYPESPECIFIC:
                            break;
                        case DBT_DEVNODES_CHANGED:
                            break;
                        case DBT_QUERYCHANGECONFIG:
                            break;
                        case DBT_USERDEFINED:
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }
        #endregion
        #region 弹出窗口

        /// <summary>
        /// 弹出提示
        /// </summary>
        /// <param name="text">内容</param>
        private void ShowMessage(string text)
        {
            ShowMessage("提示:", text);
        }
        /// <summary>
        /// 弹出提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="text">内容</param>
        private void ShowMessage(string title, string text)
        {

        }
        #endregion;
        #region 定时消息
        [DllImport("user32.dll")]
        public static extern int MessageBoxTimeoutA(IntPtr hwnd, string txt, string caption, int wtype, int wlange, int dwtimeout);
        #endregion

        internal FrmTray()
        {
            InitializeComponent();
        }
        private void FrmTrayLoad(object sender, EventArgs e)
        {

            Opacity = 0;
            this.Hide();
            this.Visible = false;
        }
        private void ExitApplication(object sender, EventArgs e)
        {
            ExitBox.ExitType result = ExitBox.Show();

            if (result != ExitBox.ExitType.NONE)
            {
                switch (result)
                {
                    case ExitBox.ExitType.MIN15:
                        notifyIconTray.ShowBalloonTip(10, "警告",
                            "实时防护已关闭,并在10分钟后开启.", ToolTipIcon.Warning);                      //
                        break;
                    case VirusCleaner.Tray.ExitBox.ExitType.HOUR1:
                        //
                        break;
                    case VirusCleaner.Tray.ExitBox.ExitType.HOUR3:
                        //
                        break;
                    case VirusCleaner.Tray.ExitBox.ExitType.HOUR8:
                        //
                        break;
                    case VirusCleaner.Tray.ExitBox.ExitType.FULLEXIT:
                        //
                        Program.ExitApplication();
                        break;


                }
            }
        }
        //菜单命令调用
        private void MenuRunApplication(object sender, EventArgs e)
        {
            Program.RunApplication(((ToolStripMenuItem)sender).Tag.ToString());
        }
        //事件直接调用
        private void TrayRunApplication(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Program.RunApplication();
            }
        }
        private static void DumpWebSite(object sender, EventArgs e)
        {
            Process.Start("http://lsongs.host.sk/", "test");
        }

        private static void AboutMe(object sender, EventArgs e)
        {
            VirusCleaner.Controls.Forms.AboutMeBox.Show();
        }

        private static void UpdateApplication(object sender, EventArgs e)
        {
            Process.Start("VirusCleanerUpdate.exe");
        }
    }
}
